package com.polaris.live.ui.live.widget

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import androidx.constraintlayout.widget.ConstraintLayout
import com.bumptech.glide.Glide
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.constant.LiveConst
import com.polaris.live.databinding.WidgetCallSendGiftBinding
import com.polaris.live.im.sfs.bean.live.payload.GiftListPayloadBean
import com.polaris.live.ui.live.widget.listener.OnGiftsSendGiftListener
import com.polaris.live.utils.image.ImageUtils

/**
 * CallSendGiftView
 *
 * @author Created by 半仙 on 2023/11/21/021 11:22
 */
class CallSendGiftView(context: Context, attrs: AttributeSet?) : ConstraintLayout(context, attrs) {

    private val mBinding = WidgetCallSendGiftBinding.inflate(LayoutInflater.from(context), this)

    private var mOnCallSendGiftList: OnGiftsSendGiftListener? = null

    fun setData(giftListPayloadBean: GiftListPayloadBean, avatar: String?, userId: Long? = null) {
        val gift = giftListPayloadBean.gifts.getOrNull(0) ?: return

        Glide.with(context).load(ImageUtils.imgPath(gift.pic))
            .into(mBinding.ivGift)
        mBinding.userAvatar.setImageUrl(avatar, userId)

        setOnSingleClickListener {
            val giftType = if (gift.type == LiveConst.GiftType.FOLLOWER_GIFT) {
                LiveConst.SendGift.followerGift
            } else {
                LiveConst.SendGift.commonGift
            }
            mOnCallSendGiftList?.onGiftsSendGiftListener(gift, giftType)
        }
    }

    fun setOnCallSendGiftList(l: OnGiftsSendGiftListener) {
        mOnCallSendGiftList = l
    }

}